package com.woniuxy.lab.woniu_lab_boss.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.lab.woniu_lab_boss.common.Result;
import com.woniuxy.lab.woniu_lab_boss.controller.form.AddTeacherForm;
import com.woniuxy.lab.woniu_lab_boss.controller.form.ChangePasswordForm;
import com.woniuxy.lab.woniu_lab_boss.exception.WebException;
import com.woniuxy.lab.woniu_lab_boss.exception.WebExceptionCode;
import com.woniuxy.lab.woniu_lab_boss.model.Account;
import com.woniuxy.lab.woniu_lab_boss.model.Teacher;
import com.woniuxy.lab.woniu_lab_boss.service.AccountService;
import com.woniuxy.lab.woniu_lab_boss.service.TeacherService;
import com.woniuxy.lab.woniu_lab_boss.service.dto.HealthDto;
import com.woniuxy.lab.woniu_lab_boss.service.dto.TeacherDto;
import com.woniuxy.lab.woniu_lab_boss.util.JwtUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 讲师表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-03-15
 */
@RestController
@Api(tags = "[K]讲师相关接口")
@RequestMapping("/teacher")
@Slf4j
public class TeacherController {

    @Resource
    private TeacherService teacherService;


    /**
     * 教师获取个人信息
     *
     * @return
     */
    @ApiOperation("60 讲师查看账号信息")
    @GetMapping("/queryMyInfo")
    public Result<TeacherDto> queryMyInfo() {
        log.info("讲师查询个人状态");
        Integer teacherId = teacherService.getTeacherId();
        Teacher teacher = teacherService.getById(teacherId);
        TeacherDto teacherDto = BeanUtil.copyProperties(teacher, TeacherDto.class);
        return Result.success(200, "获取讲师个人信息成功", teacherDto);
    }
// HttpServletRequest req

    /**
     * 讲师修改密码
     *
     * @param changePasswordForm
     * @return
     */
    @ApiOperation("61 讲师修改密码")
    @PutMapping("/changePassword")
    public Result<Object> changePass(@RequestBody @Valid ChangePasswordForm changePasswordForm) {
        log.info("讲师修改密码{}", changePasswordForm);
        Integer teacherId = teacherService.getTeacherId();
        teacherService.changePassword(teacherId, changePasswordForm.getPassword(), changePasswordForm.getNewPassword());
        return Result.success(200, "修改讲师密码成功", null);
    }

    /**
     * 查询所有讲师
     *
     * @param page
     * @param size
     * @return
     */
    @ApiOperation("72 查询所有讲师")
    @GetMapping("/queryAll")
    public Result<IPage<TeacherDto>> query(@ApiParam(value = "第几页") @Min(value = 1,message="页码必须大于1") Integer page, @ApiParam(value = "每页条数") @Min(value = 1,message="每页展示数必须大于1") @Max(value = 10,message = "每页展示数不能大于10") Integer size) {
        log.info("查询讲师{}{}", page, size);
        IPage<TeacherDto> page1 = teacherService.findAll(page, size);
        return Result.success(200, "查询所有讲师成功", page1);
    }

    /**
     * 根据keyword查询讲师
     *
     * @param condition
     * @param page
     * @param size
     * @return
     */
    @ApiOperation("73 关键词搜索讲师")
    @GetMapping("/queryTeacher")
    public Result<IPage<TeacherDto>> query(
            @ApiParam(value = "关键词") String condition,
            @ApiParam(value = "第几页") @Min(value = 1,message="页码必须大于1") Integer page,
            @ApiParam(value = "每页条数") @Min(value = 1,message="每页展示数必须大于1") @Max(value = 10,message = "每页展示数不能大于10") Integer size
    ) {
        log.info("查询讲师{}{}{}", condition, page, size);
        IPage<TeacherDto> page1 = teacherService.search(condition, page, size);
        return Result.success(200, "条件查询讲师成功", page1);
    }

    /**
     * 新增讲师
     *
     * @param
     * @return
     */
    @ApiOperation("71 新增讲师")
    @PostMapping("/addTeacher")
    public Result<Object> add(@RequestBody AddTeacherForm form) {
        String username = form.getUsername();
        String password = form.getPassword();
        log.info("新增讲师{}", username);
        teacherService.addTeacher(username, password);
        return Result.success(200, "新增讲师成功", null);
    }
//    public Result<Object> add(@ApiParam(value = "账号") @RequestParam @NotNull(message = "讲师用户名不能为空") String username, @ApiParam(value = "密码") @RequestParam @NotNull(message = "讲师密码不能为空") String password) {
//        log.info("新增讲师{}", username);
//        teacherService.addTeacher(username, password);
//        return Result.success(200, "新增讲师成功", null);
//    }

    /**
     * 修改讲师状态
     *
     * @param teacherId
     * @return
     */
    @ApiOperation("74 修改讲师状态")
    @PutMapping("/modifyStatus")
    public Result<Object> forbiddenTeacher(@ApiParam(value = "讲师id") @RequestParam
                                          @Min(value = 1,message="讲师ID不能小于1") Integer teacherId) {
        log.info("修改教师{}的状态", teacherId);
        teacherService.modifyTeacher(teacherId);
        return Result.success(200, "修改讲师状态成功", null);
    }

    /**
     * 重置讲师的密码
     *
     * @param teacherId
     * @return
     */
    @ApiOperation("75 重置讲师密码")
    @PutMapping("/resetPassword")
    public Result<Object> resetPassword(@ApiParam(value = "讲师ID") @RequestParam
                                            @Min(value = 1,message="讲师ID不能小于1")Integer teacherId) {
        log.info("重置{}讲师密码", teacherId);
        teacherService.resetPassword(teacherId);
        return Result.success(200, "重置讲师密码成功", null);
    }

    @ApiOperation("93 讲师修改个人信息")
    @PutMapping("/modifyMyInfo")
    public Result<Object> modifyInfo(@ApiParam(value = "讲师个人信息") @RequestParam @NotNull(message="讲师个人信息不能为空") String info) {
        log.info("讲师修改个人信息{}", info);
        Integer teacherId = teacherService.getTeacherId();
        Teacher byId = teacherService.getById(teacherId);
        byId.setInfo(info);
        teacherService.updateById(byId);
        return Result.success(200, "修改讲师信息成功", null);
    }
}

